package cn.learn.controller.system.role;

import cn.learn.base.BaseController;
import cn.learn.domain.module.Module;
import cn.learn.domain.system.role.Role;
import cn.learn.service.module.ModuleService;
import cn.learn.service.system.role.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/system/role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private ModuleService moduleService;

    /**
     * 分页展示全部角色
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/list", name = "展示角色")
    public String list(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        PageInfo resultData = roleService.selectAllByPage(pageNum, pageSize, companyId);
        request.setAttribute("pageList", resultData);
        return "/system/role/role-list";
    }

    /**
     * 添加一条角色 与 跳转添加页面方法
     * @param role
     * @return
     */
    @RequestMapping(value = "/addOne", name = "添加角色")
    public String addOne(Role role) {
        if (request.getMethod().equals("POST")) {
            String uid = UUID.randomUUID().toString().replace("-", "");
            role.setId(uid);
            role.setCompanyId(companyId);
            role.setCompanyName(companyName);
            roleService.addOne(role);
            return "redirect:/system/role/list.do";
        }
        return "/system/role/role-add";
    }

    /**
     * 修改页面方法 与 跳转修改页面方法
     * @param role
     * @return
     */
    @RequestMapping(value = "/update", name = "修改角色")
    public String update(Role role) {
        if (request.getMethod().equals("POST")) {
            role.setCompanyId(companyId);
            role.setCompanyName(companyName);
            roleService.updateOneById(role);
            return "redirect:/system/role/list.do";
        }
        Role resultRole = roleService.findOneById(role);
        request.setAttribute("data", resultRole);
        return "/system/role/role-update";
    }

    /**
     * 根据id删除一个角色
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", name = "删除角色")
    public String delete(String id) {
        roleService.deleteOneById(id);
        return "redirect:/system/role/list.do";
    }

    /**
     * 跳转到权限管理界面
     * @param roleid
     * @return
     */
    @RequestMapping(value = "/roleModule", name = "跳转权限管理界面")
    public String roleModule(String roleid) {
        Role role = new Role();
        role.setId(roleid);
        Role resultRole = roleService.findOneById(role);
        request.setAttribute("data", resultRole);
        return "/system/role/role-module";
    }

    /**
     * 获取所有权限以及当前用户权限 以json格式返回给前端
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/initModuleData", name = "跳转角色权限选择界面")
    public @ResponseBody List initModuleData(String roleId) {
        System.out.println(roleId);
        // 用户权限
        List<Module> roleModule = moduleService.selectRoleModule(roleId);
        System.out.println(roleModule);
        // 所有权限
        List<Module> resultModule = moduleService.selectAll();
        System.out.println(resultModule);
        // 组转成ztree需要的数据 会自动转换成json格式数据
        List<Map> ztreeList = new ArrayList();
        for (Module module : resultModule) {
            HashMap map = new HashMap();
            // 设置数据
            map.put("id", module.getId());
            map.put("pId", module.getParentId());
            map.put("name", module.getName());
            // 判断角色是否有当前菜单权限
            if (roleModule.contains(module)) {
                map.put("checked", true);
            }
            // 放入集合中
            ztreeList.add(map);
        }
        // 数据
        System.out.println(ztreeList);
        return ztreeList;
    }

    /**
     * 修改角色权限
     * @param roleId
     * @param moduleIds
     * @return
     */
    @RequestMapping(value = "/updateRoleModule", name = "修改角色权限")
    public String updateRoleModule(String roleId,String moduleIds) {
        roleService.updateRoleModules(roleId, moduleIds);
        return "redirect:/system/role/list.do";
    }

}
